package com.clouddot.baipai.ui.Dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.support.constraint.ConstraintLayout;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.clouddot.baipai.R;
import com.clouddot.baipai.http.entity.response.getIndexListResponse;
import com.clouddot.baipai.infrastructure.environment.Env;
import com.clouddot.baipai.ui.activity.SettingActivity;
import com.clouddot.baipai.ui.adapter.H_EvaluateAdapter;
import com.clouddot.baipai.util.Toast.ToastUtil;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by wxy on 2018/4/2.
 */

public class A_KeyDailog {
    private Dialog Dialog;
    private ConstraintLayout constraintLayout;
    private Context context;
    private View view;
    private Button[] btnNumber = new Button[11];
    private Button btnInput_delete;
    private String num = "";
    private TextView[] textViews = new TextView[4];

    public A_KeyDailog(Context context ) {
        this.context = context;

        initView();

    }

    private void initView() {
        if(Env.getAppContext().banPaiType == 0) {
            constraintLayout = (ConstraintLayout) LayoutInflater.from(context).inflate(R.layout.h_dialog_key, null);
        }else {
            constraintLayout = (ConstraintLayout) LayoutInflater.from(context).inflate(R.layout.v_dialog_key, null);
        }
        btnNumber[0] = constraintLayout.findViewById(R.id.btn_input_money_one);
        btnNumber[1] = constraintLayout.findViewById(R.id.btn_input_money_two);
        btnNumber[2] = constraintLayout.findViewById(R.id.btn_input_money_three);
        btnNumber[3] = constraintLayout.findViewById(R.id.btn_input_money_four);
        btnNumber[4] = constraintLayout.findViewById(R.id.btn_input_money_five);
        btnNumber[5] = constraintLayout.findViewById(R.id.btn_input_money_six);
        btnNumber[6] = constraintLayout.findViewById(R.id.btn_input_money_seven);
        btnNumber[7] = constraintLayout.findViewById(R.id.btn_input_money_eight);
        btnNumber[8] = constraintLayout.findViewById(R.id.btn_input_money_nine);
        btnNumber[9] = constraintLayout.findViewById(R.id.btn_input_money_zero);
        btnNumber[10] = constraintLayout.findViewById(R.id.btn_input_money_point);
        btnInput_delete = constraintLayout.findViewById(R.id.btn_input_money_delete);

        textViews[0] = constraintLayout.findViewById(R.id.one);
        textViews[1] = constraintLayout.findViewById(R.id.two);
        textViews[2] = constraintLayout.findViewById(R.id.three);
        textViews[3] = constraintLayout.findViewById(R.id.four);

        for (Button btn : btnNumber)
        {
            btn.setOnClickListener(onClick);
        }

        btnInput_delete.setOnClickListener(deleteClick);
    }

    public View.OnClickListener onClick = new View.OnClickListener()
    {
        @Override
        public void onClick(View view)
        {
            num = num +""+ ((Button) view).getText().toString();
            btnInput_delete.setText("删除");
            showTv(num.length());
            if(num.length() == 4 ){
                if( num.equals("1234")) {
                    closeDialog();
                    listener.onClicked();
                }else{
                    num = "";
                    ToastUtil.showToast(context,"密令错误");
                    showTv(0);
                }
            }
        }
    };

    private void showTv(int length){
        textViews[0].setVisibility(View.GONE);
        textViews[1].setVisibility(View.GONE);
        textViews[2].setVisibility(View.GONE);
        textViews[3].setVisibility(View.GONE);
        if(length == 1){
            textViews[0].setVisibility(View.VISIBLE);
        }else if(length == 2){
            textViews[0].setVisibility(View.VISIBLE);
            textViews[1].setVisibility(View.VISIBLE);
        }else if(length == 3){
            textViews[0].setVisibility(View.VISIBLE);
            textViews[1].setVisibility(View.VISIBLE);
            textViews[2].setVisibility(View.VISIBLE);
        }else if(length == 4){
            textViews[0].setVisibility(View.VISIBLE);
            textViews[1].setVisibility(View.VISIBLE);
            textViews[2].setVisibility(View.VISIBLE);
            textViews[3].setVisibility(View.VISIBLE);
        }
    }


    public View.OnClickListener deleteClick = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if(((Button) view).getText().equals("返回")){
                closeDialog();
            }else{
                if(num.length() > 1) {
                    num = num.substring(0, num.length() - 1);
                }else{
                    num = "";
                    btnInput_delete.setText("返回");
                }
                showTv(num.length());
            }
        }
    };

    public void showDialog() {

        Dialog = getDialog();
        Dialog.show();

    }

    public void closeDialog() {
        if (Dialog != null) {
            Dialog.dismiss();
        }
    }

    public Dialog getDialog() {
        if (Dialog == null) {
            Dialog = new Dialog(context, R.style.ActionDialogStyle);
        }

        Dialog.setContentView(constraintLayout);

        Window dialogWindow = Dialog.getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = WindowManager.LayoutParams.MATCH_PARENT;
        dialogWindow.setAttributes(lp);
        view = dialogWindow.getDecorView();
        lock();

        view.setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener() {
            @Override
            public void onSystemUiVisibilityChange(int i) {
                lock();
            }
        });

        Dialog.setCancelable(false);

        return Dialog;
    }

    private void lock() {
        int uiFlags = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_IMMERSIVE;
        view.setSystemUiVisibility(uiFlags);
    }

    private AdapterItemClickListener listener;

    public void setOnClickListener(AdapterItemClickListener listener) {
        this.listener = listener;
    }

    public interface AdapterItemClickListener {
        void onClicked();
    }

}



